//
//  NewsDetailViewController.m
//  Blank
//
//  Created by 周星宇 on 15/9/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "AFNetWorkingGet.h"

@interface NewsDetailViewController () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *contentWebView;
@property (nonatomic, strong) NSString *contentStr;
@property (nonatomic, strong) NSString *titleStr;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation NewsDetailViewController

- (void)dataHandle{
    NSString *url = [NSString stringWithFormat:@"%@%@", MoreNewsDetailURL, self.id];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        self.titleStr = [responseObject objectForKey:@"tit"];
        self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100 * Height)];
        [self.view addSubview:self.titleView];
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * Windth, 0, self.view.frame.size.width - 30 * Windth, 100 * Height)];
        [self.titleView addSubview:self.titleLabel];
        self.titleLabel.text = self.titleStr;
        self.titleLabel.numberOfLines = 0;
        [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:24]];

        self.contentStr = [responseObject objectForKey:@"text"];
        [self.contentWebView loadHTMLString:self.contentStr baseURL:[NSURL URLWithString:url]];
        self.contentWebView.scrollView.bounces = NO;
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"error");
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资讯详情";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10 * Windth, 100 * Height, self.view.frame.size.width - 20 * Windth, self.view.frame.size.height - 100 * Height)];
    [self.view addSubview:self.contentWebView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [AFNetWorkingGet popAlertIfNoInter:self];
    [self withNavigationItem];
    [self dataHandle];
}

- (void)withNavigationItem{
    UIButton *leftMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftMenuBtn.frame = CGRectMake(0, 0, 25 * Windth, 25 * Height);
    [leftMenuBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [leftMenuBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftMenuBtn];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if ([NightModel getColorModel]) {
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        //页面背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='gray'"];
        self.view.backgroundColor = [UIColor grayColor];
    }
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
